module.exports = (app) => {
  const Danmu = require("../models/Danmu")
  const router = require("express").Router({
    // 合并父子路由路径参数
    mergeParams: true,
  })
  //资源路径
  app.use(
    "/admin/api/danmu",
    async (req, res, next) => {
      next()
    },
    router
  )
  //获取
  router.get("/", async (req, res) => {
    let player = req.query
    let danmu = await Danmu.findOne(player)
    if (!danmu) {
      return res.send("")
    }
    let danmuList = danmu.danmuList
    res.send(danmuList)
  })
  //发送
  router.post("/", async (req, res) => {
    if (!req.session.login) return res.json({ status: -1, msg: "请先登录" })
    let body = req.body
    let danmu = await Danmu.findOne({ player: body.player })
    //新建弹幕库
    if (!danmu) {
      danmu = await Danmu.create({ player: body.player })
    }
    delete body.player
    danmu.danmuList.push(body.data)
    await danmu.save()
    res.json({ status: 0, msg: "发送成功" })
  })
}
